package com.cloudwise.gproject.controller;

import com.cloudwise.gproject.entity.Menu;
import com.cloudwise.gproject.entity.User;
import com.cloudwise.gproject.service.MenuService;
import com.cloudwise.gproject.util.Result;
import com.cloudwise.gproject.util.ReturnStatus;
import com.cloudwise.gproject.util.TreeUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 菜单权限表(Menu)表控制层
 *
 * @author makejava
 * @since 2021-03-18 18:33:47
 */
@Controller
@RequestMapping("menu")
public class MenuController {
    /**
     * 服务对象
     */
    @Resource
    private MenuService menuService;

    /**
     * 查询全部菜单，用于菜单页的展示
     * @return ReturnBean
     * @createBy Enzo
     * @createTime 2020/12/29 22:54
     */
    @ResponseBody
    @RequestMapping("queryAll")
    public Result<Menu> queryAllMenu() {
        List<Menu> menus = menuService.queryAll(new Menu());

        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0, menus);
    }

    /**
     * 查询所有菜单，用于添加和编辑菜单时的生成树
     *
     * @return List
     * @createBy Enzo
     * @createTime 2020/12/29 22:54
     */
    @ResponseBody
    @RequestMapping("queryAllMenus")
    public List queryAllMenus() {
        List<Menu> menus = menuService.queryAll(new Menu());
        System.out.println("======================");
        for (Menu menu : menus) {
            System.out.print(" "+menu.getMenuName());
        }
        System.out.println("======================");
        return TreeUtil.fromMenuListToTreeList(menus);
    }

    /**
     * 更新菜单
     *
     * @param menu 实体类
     * @return ReturnBean
     * @createBy Enzo
     * @createTime 2020/12/29 22:58
     */

    @ResponseBody
    @PutMapping("update")
    public Result updateMenu(Menu menu) {
        try {
            Menu update = menuService.update(menu);
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0, update);
        } catch (Exception e) {
            return new Result(ReturnStatus.FAIL.getCode(),
                    ReturnStatus.FAIL.getMessage(), 0, null);
        }
    }

    /**
     * 添加菜单
     *
     * @param menu 实体类
     * @return ReturnBean
     * @createBy Enzo
     * @createTime 2020/12/29 22:58
     */
    @ResponseBody
    @PostMapping("insertMenu")
    public Result insertMenu(Menu menu) {
        if (menu.getParentId() == null) {
            menu.setParentId(0);
        }
        Menu insert = menuService.insert(menu);
        if(insert != null){
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0, null);
        }
        return new Result(ReturnStatus.FAIL.getCode(),
                ReturnStatus.FAIL.getMessage(), 0, null);
    }

    @ResponseBody
    @RequestMapping("deleteById")
    public Result deleteById(Integer menuId){
        Boolean i = menuService.deleteById(menuId);
        if(i == true){
            return new Result(ReturnStatus.SUCCESS.getCode(),
                    ReturnStatus.SUCCESS.getMessage(), 0, null);
        }
        return new Result(ReturnStatus.FAIL.getCode(),
                ReturnStatus.FAIL.getMessage(), 0, null);
    }

    /**
     * 通过主键查询单条数据
     * @param id 主键
     * @return 单条数据
     */
    @ResponseBody
    @RequestMapping("selectOne")
    public Menu selectOne(Integer id) {
        return this.menuService.queryById(id);
    }
    /** 
     * @create by: 卫子健
     * @description: 跳转到后台首页，即menu.html 
     * @create time: 2021/3/18 18:38
     * @param session,model
     * @return java.lang.String 
     */
    @RequestMapping("toMenu")
    public String toMenu(HttpSession session, Model model){
        //获取成功登录的用户
        User userInfo = (User)session.getAttribute("userInfo");
        //获取用户id，用来查询菜单列表
        Integer userId = userInfo.getUserId();
        //调用service层方法，查出userID对应的一级和二级菜单列表
        List<Menu> menus = menuService.queryByPidAndUid(userId);
        //将查出的菜单列表，绑定到model传回menu.html
        model.addAttribute("menus",menus);
        return "back/menu";
    }
    @RequestMapping("toHomeIndex")
    public String toHomeIndex(){
        return "back/homeIndex";
    }

    @RequestMapping("toMenu2")
    public String toMenu2() {
        return "back/menu0";
    }

    /**
     * 根据id查找父节点
     * @param menuId 主键
     * @return ReturnBean
     * @createBy Enzo
     * @createTime 2020/12/29 22:59
     */
    @ResponseBody
    @RequestMapping("findParentNameId")
    public Result findParentNameId(Integer menuId) {
        Menu parent = menuService.findParentNameId(menuId);
        return new Result(ReturnStatus.SUCCESS.getCode(),
                ReturnStatus.SUCCESS.getMessage(), 0, parent);
    }
}
